package com.unknown.wificontrol.ui.home;

import android.view.View;
import android.widget.EditText;

import com.unknown.wificontrol.R;
import com.unknown.wificontrol.http.Http;
import com.unknown.wificontrol.http.HttpListener;
import com.unknown.wificontrol.listener.OnDialogListener;
import com.unknown.wificontrol.ui.BaseActivity;
import com.unknown.wificontrol.util.Preference;

import org.json.JSONObject;

import butterknife.BindView;

public class CustomizeSettingActivity extends BaseActivity {
    @Override
    public int getLayout() {
        return R.layout.layout_custom_setting;
    }

    @Override
    public void initViews() {
        etWriteREGAddress.setText(Preference.get(this, "WriteREGAddress"));
        etWriteREGAddress.setSelection(etWriteREGAddress.getText().toString().length());
        etWriteREGData.setText(Preference.get(this, "WriteREGData"));
        etReadREGAddress.setText(Preference.get(this, "ReadREGAddress"));
        etReadREGData.setText(Preference.get(this, "ReadREGData"));
        etDA0VOLT.setText(Preference.get(this, "DA0VOLT"));
        etDA1VOLT.setText(Preference.get(this, "DA1VOLT"));
    }

    @BindView(R.id.etWriteREGAddress)
    EditText etWriteREGAddress;

    @BindView(R.id.etWriteREGData)
    EditText etWriteREGData;

    @BindView(R.id.etReadREGAddress)
    EditText etReadREGAddress;

    @BindView(R.id.etReadREGData)
    EditText etReadREGData;

    @BindView(R.id.etDA0VOLT)
    EditText etDA0VOLT;

    @BindView(R.id.etDA1VOLT)
    EditText etDA1VOLT;

    public void onCommit(View v) {
        String WriteREGAddress = etWriteREGAddress.getText().toString();
        String WriteREGData = etWriteREGData.getText().toString();
        String ReadREGAddress = etReadREGAddress.getText().toString();
        String ReadREGData = etReadREGData.getText().toString();

        String DA0VOLT = etDA0VOLT.getText().toString();
        String DA1VOLT = etDA1VOLT.getText().toString();

        if (isEmpty(WriteREGAddress)) {
            showToast("请输入写REG地址");
            return;
        }
        if (isEmpty(WriteREGData)) {
            showToast("请输入写REG数据");
            return;
        }
        if (isEmpty(ReadREGAddress)) {
            showToast("请输入读REG地址");
            return;
        }

        if (isEmpty(ReadREGData)) {
            showToast("请输入读REG数据");
            return;
        }

        if (isEmpty(DA0VOLT)) {
            showToast("请输入DA0-VOLT");
            return;
        }

        if (isEmpty(DA1VOLT)) {
            showToast("请输入DA1-VOLT");
            return;
        }

        JSONObject object = new JSONObject();
        try {
            object.put("WriteREGAddress", WriteREGAddress);
            object.put("WriteREGData", WriteREGData);
            object.put("ReadREGAddress", ReadREGAddress);
            object.put("ReadREGData", ReadREGData);
            object.put("DA0VOLT", DA0VOLT);
            object.put("DA1VOLT", DA1VOLT);
        } catch (Exception exception) {

        }
        showProgress("设置中...");
        Http.post(getHost() + "/getValues/Customize", object.toString(), new HttpListener() {
            @Override
            public void onReturn(JSONObject jsonObject) {
                hideProgress();
                if (jsonObject != null && jsonObject.length() > 0) {
                    int code = jsonObject.optInt("code");
                    String message = jsonObject.optString("message");
                    if (code == 200) {
                        Preference.save(mActivity, "WriteREGAddress", WriteREGAddress);
                        Preference.save(mActivity, "WriteREGData", WriteREGData);
                        Preference.save(mActivity, "ReadREGAddress", ReadREGAddress);
                        Preference.save(mActivity, "ReadREGData", ReadREGData);

                        Preference.save(mActivity, "DA0VOLT", DA0VOLT);
                        Preference.save(mActivity, "DA1VOLT", DA1VOLT);
                        showSuccess("用户自定义设置成功", new OnDialogListener() {
                            @Override
                            public void onConfirm() {
                                super.onConfirm();
                                finish();
                            }
                        });
                    } else {
                        showToast(message);
                    }
                } else {
                    showToast("请求失败");
                }

            }
        });
    }
}
